﻿using System;
using System.Web.Security;
using System.Web.UI.WebControls;

namespace visinia.web.admin.pages
{
    public partial class AddUser : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
        {
            //RolesDropDown ddl = (RolesDropDown)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("ROLES");
            //TextBox userNameTextBox = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("UserName");
            //visiniaMembershipUser user = (visiniaMembershipUser)Membership.GetUser(userNameTextBox.Text);
            //user.Role = ddl.SelectedRole;        
            //Membership.UpdateUser(user);
        }
        protected void CreateUserWizard1_CreatingUser(object sender, LoginCancelEventArgs e)
        {
            if (IsValidEmail(CreateUserWizard1.Email))
            {
                if (!IsUserNameUnique(CreateUserWizard1.UserName))
                {
                    ERROR.Text = "You must enter a unique user name. This user name already exist";
                    e.Cancel = true;
                }
            }
            else
            {
                ERROR.Text = "You must enter a valid email address";
                e.Cancel = true;
            }
        }

        private bool IsUserNameUnique(string UserName)
        {
            MembershipUser user = Membership.GetUser(UserName);
            if (user == null)
                return true;
            return false;
        }

        private bool IsValidEmail(string strIn)
        {
            // Return true if strIn is in valid e-mail format.
            return System.Text.RegularExpressions.Regex.IsMatch(strIn,
                @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
        }

    }
}